var year = 2013,month=null,dept=null;
$(function(){
	fillDepartments("groups");
	$("ul#datesel li a span").bind("click",function(){
		var $this = $(this);
		month = $(this).parent().attr("month");
		getPlan(year,month,dept);		
		$this.removeClass("label-info");
		$this.addClass("label-success");
		
		var sbs = $this.parent("a").parent("li").siblings("li"); 
		$.each(sbs,function(idx,item){
    		var other = $(item).find("a span");
    		other.removeClass("label-success");
    		other.addClass("label-info");
    	});
	});
});

function fillDepartments(deptId) {
	var depts = $("#"+deptId);
	$.getJSON('admin/department/department!bytype',
			{
		      "bean.id":"root",
		      "bean.departmentType":"Manufacture"		
			},
			
		function(data, textStatus, jqXHR) {

		$.each(data, function(i, item) {
			
			var ipt = $("<button type='button' class='btn btn-info' dept='"+item['id']+"'>"+item['name']+"</button>");
			ipt.bind("click",function(){
				var $this = $(this);
				departmentClicked($(this).attr("dept"),$(this).html());
				$this.removeClass("btn-info");
				$this.addClass("btn-success");
				
				var sbs = $this.siblings("button"); 
				$.each(sbs,function(idx,item){
		    		var other = $(item);
		    		other.removeClass("btn-success");
		    		other.addClass("btn-info");
		    	});
			});
			depts.append(ipt);			
		});	
		
	});
}
function departmentClicked(id,name){
	dept = id;
	$("#deptName").html(name);
	if(month==null)return;	
	getPlan(year,month,dept);
	
}
function getPlan(year,month,dept){
	if(month==null || dept==null){		
		return;
	}
	
	$("#date").html(year+"年"+month+"月");
	var url = "views/data-view!viewplan";
	$.get(url,"year="+year+"&month="+month+"&department.id="+dept,function(data){
		$("#a1").html(data);
	});
}